package com.yld.car.market;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import cn.yld.car.db.DialogCityDB;

import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.DialogCityItem;

public class PersonProvinceActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		this.settingTitle(R.id.title, "选择省市");

		list = (ListView) findViewById(R.id.list1);
		list.setOnItemClickListener(this);
		// Intent intent = this.getIntent();
		//
		// cId = intent.getStringExtra("cId");
		// System.out.println("cId=====" + cId);
		utils = NetworkProgressUtils.getInstance();

		showCancelListener("取消选择", cancelClickListener);
		initViews();
	}

	private NetworkProgressUtils utils;
	private ListView list;

	private OnClickListener cancelClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mApp.setmProvinceInfo(null);
			SharedPreferences sh = getSharedPreferences("city_state",
					MODE_PRIVATE);
			sh.edit().putInt("city", -1).commit();

			finish();
			//initViews();
		}
	};

	private void initViews() {

		// DialogCityDB dB=new DialogCityDB();
		List<DialogCityItem> all = DialogCityDB.getAllProInfo(this);
		List<String> names = new ArrayList<String>();
		for (DialogCityItem d : all) {
			names.add(d.getName());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.base_list_items_simple, R.id.itemContent, names) {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				convertView = super.getView(position, convertView, parent);
				SharedPreferences sh = getSharedPreferences("city_state",
						MODE_PRIVATE);
				int p = sh.getInt("city", -2);
				if (p == position) {
					convertView.setBackgroundColor(Color.RED);

				} else {
					convertView
							.setBackgroundResource(android.R.drawable.list_selector_background);
				}
				return convertView;
			}
		};
		list.setAdapter(adapter);

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {
		SharedPreferences sh = getSharedPreferences("city_state", MODE_PRIVATE);
		sh.edit().putInt("city", position).commit();

		String name = adapterView.getAdapter().getItem(position).toString();

		this.mApp.setmProvinceInfo(name);
		// if ("重庆市".equals(name) || "北京市".equals(name) || "天津市".equals(name)
		// || "上海市".equals(name)) {
		//
		// this.mApp.setmProvinceInfo(name);
		// } else {
		// Intent intent = new Intent();
		// intent.setClass(this, PersonCityActivity.class);
		// // 传递省市的名称
		// intent.putExtra("name", name);
		// startActivity(intent);
		// }

		finish();

	}

}
